from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver=webdriver.Firefox()
driver.get('file:///E:/%E8%93%AC%E8%8E%B1%E5%B0%8F%E8%AF%BE2024%E6%B5%8B%E8%AF%95/%E7%AC%AC%E5%9B%9B%E9%98%B6%E6%AE%B5/Selenium%E8%87%AA%E5%8A%A8%E5%8C%96/selenium_demo/day03demo/message.html')
#准备数据
data={
    "姓名":'lili',
    "密码":'123456',
    "电子邮件":"1234567787@qq.com",
    "职业":"法律相关",
    "留言内容":"你好，学习python 自动化测试"
}
#Xpath定位元素表达式
locator={
    "姓名":'//*[@id="un"]',
    "密码":'//*[@id="pw"]',
    "男":'//*[@id="male"]',
    "女":'//*[@id="female"]',
    "电子邮件":'//*[@name="email"]',
    "职业":'//select',
    "电脑网络":'//*[@*="computer"]',
    "影视娱乐":'//*[@*="film"]',
    "棋牌游戏":'//*[@*="chess"]',
    "读书读报":'//*[@*="read"]',
    "美酒佳肴":'//*[@*="food"]',
    "绘画书法":'//*[@*="painting"]',
    "留言内容":'//textarea',
    "提交":'//*[@id="sub"]',
    "重置":'//*[@type="reset"]'
}
#定位页面元素
myname=driver.find_element(By.XPATH,locator["姓名"])
mypassword=driver.find_element(By.XPATH,locator["密码"])
mymale=driver.find_element(By.XPATH,locator["男"])
myfemale=driver.find_element(By.XPATH,locator["女"])
mymail=driver.find_element(By.XPATH,locator["电子邮件"])
myprofession=driver.find_element(By.XPATH,locator["职业"])
mycomputer=driver.find_element(By.XPATH,locator["电脑网络"])
myfilm=driver.find_element(By.XPATH,locator["影视娱乐"])
mychess=driver.find_element(By.XPATH,locator["棋牌游戏"])
myread=driver.find_element(By.XPATH,locator["读书读报"])
myfood=driver.find_element(By.XPATH,locator["美酒佳肴"])
mypainting=driver.find_element(By.XPATH,locator["绘画书法"])
mycomments=driver.find_element(By.XPATH,locator["留言内容"])
mysubmit=driver.find_element(By.XPATH,locator["提交"])
#操作页面元素
myname.send_keys(data["姓名"])
mypassword.send_keys(data["密码"])
mymale.click()
mymail.send_keys(data["电子邮件"])
myprofession.send_keys(data["职业"])
mycomputer.click()
myfilm.click()
mychess.click()
myread.click()
myfood.click()
mypainting.click()
mycomments.send_keys(data["留言内容"])
sleep(5)
mysubmit.click()
sleep(3)
driver.quit()